package com.gu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gu.entity.StatisticsDaily;
import com.gu.entity.vo.SearchVo;
import com.gu.service.StatisticsDailyService;
import com.gu.service.UcenterClient;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * Description:
 *
 * @Autor: Rolin
 * datetime: 06-23-17:56
 */
@RestController
@CrossOrigin
@Api(tags = "数据统计组件")
@RequestMapping("/eduData")
public class DataController {

    @Resource
    private StatisticsDailyService statisticsDailyService;

    @Resource
    private UcenterClient ucenterClient;

    @ApiOperation("获取注册数据统计")
    @PostMapping("/getStatistics/{dateTime}")
    public CommonResult getStatistics(@PathVariable("dateTime") String dateTime) {
        //先查后改
        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.eq("date_calculated", dateTime);
        StatisticsDaily one = statisticsDailyService.getOne(wrapper);
        Integer registerNum = (Integer) ucenterClient.getRegisterNum(dateTime).getData().get("num");

        System.out.println(dateTime + "日注册" + registerNum + "人");
        if (one == null) {
            //为空就创建一个实体类
            StatisticsDaily statisticsDaily = new StatisticsDaily();

            Integer loginNum = RandomUtils.nextInt(500, 1000);
            Integer videoViewNum = RandomUtils.nextInt(500, 1000);
            Integer courseNum = RandomUtils.nextInt(500, 1000);

            statisticsDaily.setLoginNum(loginNum);
            statisticsDaily.setRegisterNum(registerNum);
            statisticsDaily.setVideoViewNum(videoViewNum);
            statisticsDaily.setCourseNum(courseNum);
            statisticsDaily.setDateCalculated(dateTime);

            statisticsDailyService.save(statisticsDaily);
        } else if(one.getRegisterNum()!= registerNum) {
            //否则就是更新！
            wrapper.eq("register_num", registerNum);
            statisticsDailyService.update(wrapper);
        }

        return CommonResult.success();
    }

    @ApiOperation("根据日期查询")
    @PostMapping("/create/table")
    public CommonResult createTable(@RequestBody SearchVo searchVo) {
        String begin = searchVo.getBegin();
        String type = searchVo.getType();
        String end = searchVo.getEnd();

        QueryWrapper<StatisticsDaily> wrapper = new QueryWrapper<>();
        wrapper.between("date_calculated", begin, end);
        wrapper.orderByAsc("date_calculated");
        List<StatisticsDaily> list = statisticsDailyService.list(wrapper);

        //构建传输载体：
        Map<String, Object> map = new HashMap<>();
        //横坐标日期
        List<String> dateList = new ArrayList<>();
        //纵坐标数量
        List<Integer> dataList = new ArrayList<>();

        list.forEach(statisticsDaily -> {
            dateList.add(statisticsDaily.getDateCalculated());
            if (type.equals("login_num")) {
                dataList.add(statisticsDaily.getLoginNum());
            } else if (type.equals("register_num")) {
                dataList.add(statisticsDaily.getRegisterNum());
            } else if (type.equals("video_view_num")) {
                dataList.add(statisticsDaily.getVideoViewNum());
            } else if (type.equals("course_num")) {
                dataList.add(statisticsDaily.getCourseNum());
            }
        });

        map.put("dateList",dateList);
        map.put("dataList",dataList);

        return map.size()>0 ? CommonResult.success().data("map",map) : CommonResult.error();
    }


}